package com.tanhua.controller;

import com.tanhua.interceptor.UserHolder;
import com.tanhua.model.vo.PageResult;
import com.tanhua.server.CommentsService;
import com.tanhua.server.UserFreezeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @ClassName: com.tanhua.controller.CommentsController
 * @Description:
 * @author: Li
 * @date: 2021/9/26 17:56
 * a
 */

@RestController
@RequestMapping("/comments")
public class CommentsController {

    @Autowired
    private CommentsService commentsService;
    @Autowired
    private UserFreezeService userFreezeService;

    /**
     * 添加评论
     *
     * @param map
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> saveComments(@RequestBody Map map) {
        //判断是否被冻结
        userFreezeService.checkUserStatus("2", UserHolder.getUserId(), "您已被禁言，详情请联系管理员");

        String movementId = map.get("movementId").toString();
        String comment = map.get("comment").toString();

        commentsService.saveComments(movementId, comment);
        return ResponseEntity.ok(null);
    }

    /**
     * 查询评论列表
     *
     * @param page
     * @param pagesize
     * @param movementId
     * @return
     */
    @GetMapping
    public ResponseEntity<PageResult> findComments(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize,
            String movementId) {
        PageResult pageResult = commentsService.findComments(movementId, page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    //评论喜欢
    @GetMapping("/{id}/like")
    public ResponseEntity<Integer> commentsLike(@PathVariable String id) {
        Integer count = commentsService.commentsLike(id);
        return ResponseEntity.ok(count);
    }
    //评论不喜欢
    @GetMapping("/{id}/dislike")
    public ResponseEntity<Integer> commentsDisLike(@PathVariable String id) {
        Integer count = commentsService.commentsDisLike(id);
        return ResponseEntity.ok(count);
    }



}
